//import org.apache.calcite.adapter.csv.CsvSchema;
//import org.apache.calcite.adapter.csv.CsvTable;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.tools.FrameworkConfig;
import org.apache.calcite.tools.Frameworks;
import org.apache.calcite.tools.RelBuilder;
import org.apache.calcite.tools.RelRunners;

import java.io.File;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class CalciteRelBuilderCase {
    public static void main(String[] args) throws SQLException {
        /*
        SchemaPlus rootSchema = Frameworks.createRootSchema(true);
        String csvPath = "D:\\study\\codespace\\testcalcite\\src\\main\\resources\\db";
        CsvSchema csvSchema = new CsvSchema(new File(csvPath), CsvTable.Flavor.SCANNABLE);
        rootSchema.add("consumers", csvSchema.getTable("consumers"));
        rootSchema.add("orders", csvSchema.getTable("orders"));

        FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder()
                .parserConfig(SqlParser.Config.DEFAULT)
                .defaultSchema(rootSchema)
                .build();

        RelBuilder relBuilder = RelBuilder.create(frameworkConfig);
//        id,goods,price,amount,user_id               orders
//        id,firstname,lastname,birth                 consumers
        RelNode node = relBuilder
                .scan("consumers")
                .scan("orders")
                .join(JoinRelType.INNER,
                        relBuilder.call(SqlStdOperatorTable.EQUALS,
                                relBuilder.field("id"),
                                relBuilder.field("user_id")))
                .filter(
                        relBuilder.call(SqlStdOperatorTable.EQUALS,
                                relBuilder.field("lastname"),
                                relBuilder.literal("jacky")))
                .project(
                        relBuilder.field("id"),
                        relBuilder.field("goods"),
                        relBuilder.field("price"),
                        relBuilder.field("firstname"),
                        relBuilder.field("lastname"))
                .sortLimit(0, 5, relBuilder.field("id"))
                .build();

        System.out.println(RelOptUtil.toString(node));




        System.out.println("[result:]");
        PreparedStatement run = RelRunners.run(node);
        ResultSet resultSet = run.executeQuery();

        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int colCount = resultSetMetaData.getColumnCount();
        for(int i = 1; i <= colCount ; i++){
            System.out.print(resultSetMetaData.getColumnName(i)+"\t");
        }
        System.out.println();

        while(resultSet.next()){
            for(int i = 1; i <= colCount ; i++){
                System.out.print(resultSet.getString(i)+"\t");
            }
            System.out.println();
        }
        */
    }
}